package com.ruoyi.web.controller.dm;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.dm.domain.SpaceTargetMeasureParam;
import com.ruoyi.dm.domain.SpaceTargetMeasureVO;
import com.ruoyi.dm.service.ISpaceTargetMeasureService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.List;

/**
 * 空间目标测量数据
 * **/
@Slf4j
@RestController
@RequestMapping("/collect/measure")
@Api(tags = "数据采集-空间目标测量数据")
public class SpaceTargetMeasureController extends BaseController {

    @Autowired
    private ISpaceTargetMeasureService measureService;

    /**
     * 检索
     * @param param
     * @return
     */
    @ApiOperation("数据查询")
    @PostMapping("/query")
    public R<IPage<SpaceTargetMeasureVO>> query(@ApiParam(name ="param",value="文件检索参数对象",required = false)@Validated @RequestBody SpaceTargetMeasureParam param) {
        IPage<SpaceTargetMeasureVO> query = measureService.query(param);
        return R.ok(query);
    }

    /**
     * 导入
     * @param files
     * @return
     */
    @PostMapping ("/upload")
    @ApiOperation("数据上传")
    @ApiImplicitParam(value = "上传文件",name = "files",dataTypeClass = File.class,required = true)
    public R<String> input(@RequestPart MultipartFile[] files) {
        try {
            measureService.input(files);
            return R.ok("成功");
        }catch (Exception e) {
            log.error("",e);
            return R.fail("导入失败:"+e.getMessage());
        }
    }

    /**
     * 文件导出
     * @return
     */
    @ApiOperation("数据导出")
    @PostMapping("/export")
    public void   export(HttpServletResponse response) {
        String s = measureService.export();
        File file = new File(s);
        try {
            response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
            FileUtils.setAttachmentResponseHeader(response, file.getName());
            FileUtils.writeBytes(s, response.getOutputStream());
        }catch (Exception e) {
            log.error("",e);
            log.error("下载失败",e);
        }finally {
            file.delete();
        }
    }

    /**
     * 导出模板下载
     */
    @GetMapping("/export/model")
    @ApiOperation("导出模板下载")
    public void  modelExport(HttpServletResponse response) {
        String s = measureService.modelExport();
        File file = new File(s);
        try {
            response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
            FileUtils.setAttachmentResponseHeader(response, file.getName());
            FileUtils.writeBytes(s, response.getOutputStream());
        }catch (Exception e) {
            log.error("",e);
            log.error("下载失败",e);
        }
    }


    /**
     * 删除
     * @param id
     * @return
     */
    @GetMapping("/del")
    @ApiOperation("数据删除(单个)")
    public R<Boolean>  del(@ApiParam(value = "id", name = "id") @RequestParam(value = "id", required = true) Long id) {
        return R.ok(measureService.delFile(id));
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PostMapping("/delBatch")
    @ApiOperation("数据删除(批量)")
    public R<Boolean>  delBath(@ApiParam(value = "id集合", name = "ids")@RequestBody List<Long> ids) {
        return R.ok(measureService.delBatch(ids));
    }


    /**
     * 新增 或 编辑
     */
    @PostMapping("/edit")
    @ApiOperation("数据新增/编辑")
    public R<String> edit(@ApiParam @RequestBody SpaceTargetMeasureVO vo) {
        measureService.edit(vo);
        return R.ok("");
    }
}
